﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Odyssey.Web.Models
{
    public class LocalizedRequiredAttribute : RequiredAttribute, IClientValidatable
    {
        public LocalizedRequiredAttribute(string errorMessageCode)
        {
            this.ErrorMessage = errorMessageCode;//.GetResourceText();
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var modelClientValidationRule = new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(metadata.DisplayName),
                ValidationType = "required"
            };

            yield return modelClientValidationRule;
        }
    }

    public class LocalizedRegularExpressionAttribute : RegularExpressionAttribute, IClientValidatable
    {
        public LocalizedRegularExpressionAttribute(string pattern, string errorMessageCode)
            : base(pattern)
        {
            this.ErrorMessage = errorMessageCode;//.GetResourceText();
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var modelClientValidationRule = new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(metadata.DisplayName),
                ValidationType = "regex",
            };
            modelClientValidationRule.ValidationParameters.Add("pattern", this.Pattern);

            yield return modelClientValidationRule;
        }
    }
}
